<?php
/**
 * 上传接口
 */


require "login_filter.php";
//require dirname(__DIR__)."/common.php";

use app\cls\tom\Config;
use app\cls\tom\Response;
use app\cls\upload\FileUploadException;
use app\cls\upload\SingleUpload;
use app\cls\tom\FFmpeg;


if(empty($_REQUEST["upload_type"])) {
    Response::output(-1, "参数错误");
}
$redis = \app\cls\tom\SysResource::getRedis();
//判断是否支持该类型上传
$allUploadConfig = Config::load("cos_upload");
if(!isset($allUploadConfig[$_REQUEST["upload_type"]])){
    Response::output(-1,"can't support the upload type {$_REQUEST["upload_type"]}!");
}
$uploadConfig = $allUploadConfig[$_REQUEST["upload_type"]];
if(empty($uploadConfig)){
    Response::output(-1,"不支持该类型",["error_field"=>"upload_type"]);
}
$uploadConfig["redis"] = $redis;
try{
    $ffmpeg=new FFmpeg();
    //开始上传
    $singleUpload = new SingleUpload($uploadConfig);
    $singleUpload->douploads();
    $fileInfo = $singleUpload->getFileDataInfo();

    //判断视屏是否被旋转过
//    $xuanzhuan=$ffmpeg->getVideoDetail($fileInfo["file_path"],2);
//    if(!empty($xuanzhuan)){
//        foreach($xuanzhuan['streams'] as $k=>$v){
//            if($v['codec_type']=="video"){
//                if($v['side_data_list'] && $v['side_data_list']['rotation']=="-90"){
//                    $out_path=upload_dir();
//                    $xuanzhuan=$ffmpeg->xuanzhaun($fileInfo["file_path"],$out_path['savepath']);
//                }
//            }
//        }
//    }else{
//        $img_path=upload_dir();
//        $re=$ffmpeg->getCoverFromVideo1($fileInfo["file_path"],$img_path['savepath']);
//    }
    $img_path=upload_dir();
    $re=$ffmpeg->getCoverFromVideo1($fileInfo["file_path"],$img_path['savepath']);
    if($re){
        $fileInfo["savehttp"]=$img_path['savehttp'];
        Response::output(0,"上传文件成功", array_merge($fileInfo, ["access_url"=>$fileInfo["file_path"]]),$_SERVER['SERVER_ADDR']);
    }else{
        Response::output(-1,"截图失败",$ffmpeg->getLastError(),$_SERVER['SERVER_ADDR']);
    }
//    Response::output(0,"上传文件成功", array_merge($fileInfo, ["access_url"=>$fileInfo["file_path"]]));
}catch (FileUploadException $e){
    Response::output(-1,"上传文件失败:【msg===>{$e->getMessage()}】");
}



function getMsecTime()
{
    list($msec, $sec) = explode(' ', microtime());
    $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    return $msectime;
}
function upload_dir($type="tmp1",$suffix="jpg"){
    //解决跨域问题
    $allUploadConfig = Config::load("cos_upload");

    $uploadConfig = $allUploadConfig[$type];

    //创建上传目录，配置上传
    $date1=date("Y",time());
    $date2=date("m",time());
    $date3=date("d",time());
    $dir="/".$date1."/".$date2."/".$date3;
    $uploadpath1=$uploadConfig['savepath'];
    $uploadpath2=$uploadConfig['savehttp'];
    $key1=rand(1000,10000);
    $key2=rand(10000,20000);
    $key3=rand();
    $file_name=$key1.$key2.$key3;
    //这是服务器上的上传地址
    $uploadpath['savepath']=$uploadpath1.$dir."/".$file_name.".{$suffix}";
    if($type=="video"){
        mkdir($uploadpath1.$dir."/".$file_name,0777,true);
    }
    //这是网络访问地址
    $uploadpath['savehttp']=$uploadpath2.$dir."/".$file_name.".{$suffix}";
    return $uploadpath;
}
